clear
clc

addpath(genpath('Fig_II'))


defpos = [100         100        1500         700];
set(0,'defaultfigurepos',defpos);


set(0,'defaultlinelinewidth',2.5);
set(0,'defaultaxesfontsize',14); 
set(0,'defaulttextfontsize',12);  
set(0,'defaultlinelinewidth',2.5);

CalibratedValuesFileName='params\CalibratedParameters';
      


[param,paranames]=xlsread(CalibratedValuesFileName,'param');

%% Output folder for figures
Figures_path=strcat('.\Figures');


    %% Panel Figure: Propagation of Funded and Unfunded fiscal shocks in production economies   
    %Row 1   IRF RBC model with short term bonds only (Duration AD=1)
    
    
    param0=param;
    [GG,RR,eu,SDX,flag,ACont,ALag,ALead,PSI] = mod_production_economy_SSrules(param0,1);
   
    nirf=10;            % impulse response horizon
    nstates=size(GG,1); 
    nshocks=size(RR,2); 

   
    %Set up a bysection algorithm to iterate on the standard deviation of the unfunded transfer shock to
    %get an impact response of inflation equal to 1 as in the Fisherian model
    
    delta=1; % initialize accuracy paramameter
    SDX_R=SDX; SDX_L=SDX; SDX_M=SDX;
    SDX_L(2,2)=0;
while abs(delta)>10^(-8)
     
     SDX_M(2,2)=(SDX_L(2,2)+SDX_R(2,2))/2;
     SDX_M(1,1)=SDX_M(2,2);   % set the stdev of funded shock to equal stdev of unfunded shock
    for jj=1:2
        ssirf=zeros(nstates,nirf);
        imp=zeros(nshocks,1);
        imp(jj)=-1;
        ssirf(:,1)=RR*SDX_M*imp*100;
        for t=1:nirf-1
            ssirf(:,t+1)=GG*ssirf(:,t);
        end
        ssirf(abs(ssirf)<1e-8)=0;
        SSIRF(:,:,jj)=ssirf;
    end
    delta=SSIRF(6,1,2)-1;
   if delta>0
       SDX_R(2,2)=SDX_M(2,2);
   else SDX_L(2,2)=SDX_M(2,2);
   end
end

    figure('name','Panel')
    subplot(3,4,1)
    plot(1:1:nirf,SSIRF(6,:,1),'b');
    hold on
    plot(1:1:nirf,SSIRF(6,:,2),'-- r');
    hold off
    axis tight
    ylim([0 1])
    xticks(0:2:10);
    h1=legend('Funded','Unfunded' );
    legend box off
    set(h1,'FontSize',8)
    g1=ylabel('Flexible prices');
    set(g1,'FontSize',10)
    title('Inflation')
    grid on
    
    subplot(3,4,2)
    plot(1:1:nirf,SSIRF(1,:,1),'b');
    hold on
    plot(1:1:nirf,SSIRF(1,:,2),'-- r');
    hold off
    axis tight
    title('Output')
    grid on
     
    subplot(3,4,3)
    plot(1:1:nirf,SSIRF(8,:,1),'b');
    hold on
    plot(1:1:nirf,SSIRF(8,:,2),'-- r');
    hold off
    axis tight
    ylim([-1 1])
    title('Debt to GDP')
    grid on
    
    subplot(3,4,4)
    plot(1:1:nirf,SSIRF(4,:,1),'b');
    hold on
    plot(1:1:nirf,SSIRF(4,:,2),'-- r');
    hold off
    axis tight
    ylim([-0.2 0.2])
    title('Ex-ante real rate')
    grid on
    %-------------------------------------------------------
    %Row 2  Generate IRFs of RBC model with PhiF=0.8 and Duration AD=24
    param0=param;
    param0(10)=0.8;
    param0(4)= 24;
   
    [GG,RR,eu,SDX,flag,ACont,ALag,ALead,PSI] =  mod_production_economy_SSrules(param0,1);
    SDX=SDX_M;
    for jj=1:2
        ssirf_dur_phiF=zeros(nstates,nirf);
        imp=zeros(nshocks,1);
        imp(jj)=-1;
        ssirf_dur_phiF(:,1)=RR*SDX*imp*100;
        for t=1:nirf-1
            ssirf_dur_phiF(:,t+1)=GG*ssirf_dur_phiF(:,t);
        end
        ssirf_dur_phiF(abs(ssirf_dur_phiF)<1e-8)=0;
        SSIRF_dur_phiF(:,:,jj)=ssirf_dur_phiF;
    end
    
    
    %Row 2  Compute IRFs for RBC model with PhiF=0.8 and AD=1
     param0=param;
     param0(10)=0.8;
    [GG,RR,eu,SDX,flag,ACont,ALag,ALead,PSI] =  mod_production_economy_SSrules(param0,1);
    SDX=SDX_M;
    for jj=1:2
        ssirf=zeros(nstates,nirf);
        imp=zeros(nshocks,1);
        imp(jj)=-1;
        ssirf(:,1)=RR*SDX*imp*100;
        for t=1:nirf-1
            ssirf(:,t+1)=GG*ssirf(:,t);
        end
        ssirf(abs(ssirf)<1e-8)=0;
        SSIRF(:,:,jj)=ssirf;
    end
    
    
  
    subplot(3,4,5)
    plot(1:1:nirf,SSIRF(6,:,1),'b');
    hold on
    plot(1:1:nirf,SSIRF(6,:,2),'-- r');
    hold on
    plot(1:1:nirf,SSIRF_dur_phiF(6,:,2),': m');
    hold off
    axis tight
    ylim ([0 1])
    h1=legend('Funded','Unfunded','Unfunded + maturity' );
    legend box off
    set(h1,'FontSize',8)
    g1=ylabel('Flexible prices + {\phi^F}=0.8');
    set(g1,'FontSize',10)
    grid on
    
    subplot(3,4,6)
    plot(1:1:nirf,SSIRF(1,:,1),'b');
    hold on
    plot(1:1:nirf,SSIRF(1,:,2),'-- r');
    hold on
    plot(1:1:nirf,SSIRF_dur_phiF(1,:,2),': m');
    hold off
    axis tight
    grid on
     
    subplot(3,4,7)
    plot(1:1:nirf,SSIRF(8,:,1),'b');
    hold on
    plot(1:1:nirf,SSIRF(8,:,2),'-- r');
    hold on
    plot(1:1:nirf,SSIRF_dur_phiF(8,:,2),': m');
     hold off
    axis tight
    ylim([-1 1])
    grid on
    
    subplot(3,4,8)
    plot(1:1:nirf,SSIRF(4,:,1),'b');
    hold on
    plot(1:1:nirf,SSIRF(4,:,2),'-- r');
    hold on
    plot(1:1:nirf,SSIRF_dur_phiF(4,:,2),':m');
    hold off
    axis tight
    ylim([-0.2 0.2])
    grid on

    %-------------------------------------------------------
    %Row 3  Generate IRFs of NK model with PhiF=0 and Duration AD=1
    param0=param;
    [GG,RR,eu,SDX,flag,ACont,ALag,ALead,PSI] =  mod_production_economy_SSrules(param0,2);
    SDX=SDX_M;
    for jj=1:2
        ssirf=zeros(nstates,nirf);
        imp=zeros(nshocks,1);
        imp(jj)=-1;
        ssirf(:,1)=RR*SDX*imp*100;
        for t=1:nirf-1
            ssirf(:,t+1)=GG*ssirf(:,t);
        end
        ssirf(abs(ssirf)<1e-8)=0;
        SSIRF(:,:,jj)=ssirf;
    end
    
    subplot(3,4,9)
    plot(1:1:nirf,SSIRF(6,:,1),'b');
    hold on
    plot(1:1:nirf,SSIRF(6,:,2),'-- r');
    hold off
    axis tight
    ylim([0 1])
    g1=ylabel('Nominal Rigidities');
    set(g1,'FontSize',10)
    grid on
    
    subplot(3,4,10)
    plot(1:1:nirf,SSIRF(1,:,1),'b');
    hold on
    plot(1:1:nirf,SSIRF(1,:,2),'-- r');
    hold off
    axis tight
      ylim([-1 1])
     grid on
    
    subplot(3,4,11)
    plot(1:1:nirf,SSIRF(8,:,1),'b');
    hold on
    plot(1:1:nirf,SSIRF(8,:,2),'-- r');
    hold off
    axis tight 
       ylim([-1 1])
    grid on
    
    subplot(3,4,12)
    plot(1:1:nirf,SSIRF(4,:,1),'b');
    hold on
    plot(1:1:nirf,SSIRF(4,:,2),'-- r');
    hold off
    axis tight
        ylim([-0.2 0.2])
     grid on
    tightfig;
    
   
savefigure_pdf(strcat(Figures_path,'\Figure_II'));
